package com.example.sunmiv1.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.GoodBean;
import com.example.sunmiv1.views.MyGridview;
import com.google.gson.Gson;

import java.util.List;
import java.util.Objects;

import butterknife.BindView;
import butterknife.ButterKnife;

public class SkuAdapter extends BaseAdapter {
    private LayoutInflater inflater;
    private GoodBean.DataBean.GoodsBean skuList;
    private Context mContext;
    private onListener onListener;
    private GoodBean.DataBean.GoodsBean mSelect;
    private boolean mClick=false;

    public SkuAdapter(Context context, GoodBean.DataBean.GoodsBean skuList,GoodBean.DataBean.GoodsBean select) {
        inflater = LayoutInflater.from(context);
        this.skuList = skuList;
        this.mContext = context;
        mSelect=select;
    }

    @Override
    public int getCount() {
        return skuList.getSkuinfo().size();
    }

    @Override
    public Object getItem(int i) {
        return skuList.getSkuinfo().get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder vh;
        if (view == null) {
            view = inflater.inflate(R.layout.specification_item, null);
            vh = new ViewHolder(view);
            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }
        vh.skuName.setText(skuList.getSkuinfo().get(i).getName());
        GoodBean.DataBean.GoodsBean.SkuAttrBean item=skuList.getSkuinfo().get(i);
        final SkuKindAdapter skuKindAdapter = new SkuKindAdapter(mContext, skuList.getSkuinfo().get(i).getInfo());
        vh.kindGridView.setAdapter(skuKindAdapter);
        final int index = i;
        if(mSelect!=null && mSelect.getSkujson()!=null && mSelect.getSkuAttr()!=null && !mClick){
            skuKindAdapter.getPosition(1000);
            //Log.e(getClass().getSimpleName(), "getView: json="+(new Gson().toJson(mSelect)));
            if(!mSelect.getSkujson().isEmpty()){
                String[] idList=mSelect.getSkujson().get(0).sku_id_list.split(",");
                if(idList.length>i){
                    int select=skuKindAdapter.getPositionId(idList[i]);
                }
            }
        }else {
            skuKindAdapter.getPosition(1000);
        }

        vh.kindGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                mClick=true;
                skuKindAdapter.getPosition(i);
                onListener.getPosition(index, skuList.getSkuinfo().get(index).getInfo().get(i).getPath());
            }
        });
        return view;
    }

    private String TAG=getClass().getSimpleName()+"-CollectingSilverActivity";
    public void onClicked(onListener onListener) {
        this.onListener = onListener;
        if(mSelect!=null && mSelect.getSkujson()!=null && mSelect.getSkuAttr()!=null && !mClick){
            Log.e(getClass().getSimpleName(), "getView: json="+(new Gson().toJson(mSelect)));
            if(!mSelect.getSkujson().isEmpty()){
                String[] idList=mSelect.getSkujson().get(0).sku_id_list.split(",");
                for (int i = 0; i < skuList.getSkuinfo().size(); i++) {
                    List<GoodBean.DataBean.GoodsBean.SkuAttrBean.ListBean> detailList=skuList.getSkuinfo().get(i).getInfo();
                    if(idList.length>i){
                        int select=-1;
                        Log.e(TAG, "onClicked: for ");
                        for (int i1 = 0; i1 < detailList.size(); i1++) {
                            if(Objects.equals(detailList.get(i1).getPath(),idList[i])){
                                select=i1;
                            }
                        }
                        Log.e(TAG, "getView: select="+select);
                        if(select>=0){
                            this.onListener.getPosition(i, skuList.getSkuinfo().get(i).getInfo().get(select).getPath());
                        }
                    }
                }
            }
        }
    }

    public interface onListener {
        void getPosition(int position, String path);
    }

    static class ViewHolder {
        @BindView(R.id.sku_name)
        TextView skuName;
        @BindView(R.id.kind_gridView)
        MyGridview kindGridView;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
